package me.magicall.db.sql.expression;

import me.magicall.db.sql.Database;
import me.magicall.db.sql.Domain;

/**
 * 函数表达式
 * 
 * @author MaGiCalL
 */
public abstract class FunctionExpression extends Expression<Database> {

	private Expression<?> expression;

	protected FunctionExpression() {
		super();
	}

	protected FunctionExpression(final Expression<?> expression) {
		super();
		setName(initFunctionName());
		setExpression(expression);
	}

	protected FunctionExpression(final String name, final Database database) {
		super();
		setName(initFunctionName());
		setDomain(database);
	}

	protected String initFunctionName() {
		return getClass().getSimpleName();
	}

	@Override
	public StringBuilder appendTo(final StringBuilder sb) {
		sb.append(" ");
		final Domain domain = getDomain();
		if (domain != null) {
			domain.appendTo(sb).append(".");
		}
		sb.append(getName()).append("(");
		return getExpression().appendTo(sb).append(")");
	}

	public Expression<?> getExpression() {
		return expression;
	}

	public void setExpression(final Expression<?> expression) {
		this.expression = expression;
	}
}